/*
 * Copyright (c) 2016-2021, Hangzhou Qulian Technology Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#ifndef _SERVICE_H_
#define _SERVICE_H_


#include <string.h>
#include "ql_type.h"
#include "log.h"
#include "json.h"
#include "ql_error_type.h"
#include "contract.h"
#include <curl/curl.h>
#include "utils.h"

// typedef ql_err_t response_err_t;



//The basic Response structure, which is the information that all receipts have, specifically defines the structure for easy extension
typedef struct {
    ql_byte_t* jsonrpc;
    ql_int64_t id;
    ql_int32_t code;
    ql_byte_t* message;
    ql_byte_t* nameSpace;
}basic_response_t;

//-----------------------------Initialize the structure with JSON------------------------------//
/*Basic_rsp_from_json, initialize the basic_response_t structure with the CJSON structure
** Input: items, CJSON structure, which contains basic information about response
** Output: basicrsp, the structure to initialize
** return RESPONSE_SUCCESS,
** Parameter error returns RESPONSE_ERR_PARAM_INVALID*/
response_err_t BasicRspFromString(basic_response_t *const basicrsp, cJSON **result, ql_byte_t* str);

char* hpcAdapter(char* method, char* from, char* to, ql_int32_t value, char* payload, ql_uint64_t timestamp, ql_int32_t nonce, char* extra, char* extraId, char* signature);

char* ethAdapter(ql_int32_t nonce, ql_int64_t gasPrice, ql_int64_t gas, char* to, ql_int64_t value, char* payload, char *priv);

void sendToBlockchain(ql_byte_t* url, ql_strings_t* post_Body, ql_strings_t **recvbuf);

char* getReceipt(char* url, char* txhash);

char *pollingReceipt(char* url, char *txHash);

//free content in basicrsp and init it but not destroy basicrsp
void CleanBasicrsp(basic_response_t * basicrsp);

void BasicRspInit(basic_response_t * const basicrsp);

#if defined(unix) || defined(__linux__) || defined(__unix__) || defined(__unix) || \
    (defined(__APPLE__) && defined(__MACH__)) || defined(uECC_POSIX)
#ifdef OpenHarmony_SYS
void setCurrentTime(void);
#endif
void setTimestamp(long long *dst);
#endif
#endif